#include "kdcchaptereditorplugin.h"

#include <KPluginFactory>
#include <KPluginLoader>

class KDCChapterEditorPlugin;

#include <KLocale>
#include <kdclib.h>

K_PLUGIN_FACTORY( KDCChapterEditorPluginFactory, registerPlugin< KDCChapterEditorPlugin >(); )
K_EXPORT_PLUGIN( KDCChapterEditorPluginFactory( "KDCChapterEditorPlugin" ) )

#include <interfaces/kdcmediaplayerfactoryinterface.h>
#include <interfaces/kdcmediaplayerinterface.h>
KDCMediaPlayerInterface *gKDCMediaPlayer;
#include <interfaces/kdcjobsinterface.h>
KDCJobsInterface *gKDCJobsInterface;
#include <interfaces/kdcstatusbarinterface.h>
KDCStatusBarInterface *gKDCStatusBar;

KDCChapterEditorPlugin::KDCChapterEditorPlugin( QObject *parent, const QVariantList &args ) : KDCPluginInterface( parent ), pInfo( KPluginInfo("kdcchaptereditorplugin.desktop", "services") ), pInterfaces( NULL ), pMainWidget( NULL )
{
	gKDCMediaPlayer = NULL;
	gKDCJobsInterface = NULL;
}

KDCChapterEditorPlugin::~KDCChapterEditorPlugin()
{
	unload();
}

QString KDCChapterEditorPlugin::interfaceName() const
{
	return "KDCChapterEditorPlugin";
}

const KPluginInfo& KDCChapterEditorPlugin::info() const
{
	return pInfo;
}

QWidget* KDCChapterEditorPlugin::widget() const
{
	return pMainWidget;
}

KDCConfigPageInterface* KDCChapterEditorPlugin::configPage() const
{
	return NULL;
}

KDC::Position KDCChapterEditorPlugin::tabPosition() const
{
	return KDC::Bottom;
}

bool KDCChapterEditorPlugin::load()
{
	if ( pMainWidget )
		return false;

	pMainWidget = new KDCChapterEditor;
	return true;
}

bool KDCChapterEditorPlugin::unload()
{
	deletePtr( pMainWidget );
	return true;
}

bool KDCChapterEditorPlugin::init()
{
	foreach( KDCBaseInterface *interface, *pInterfaces )
		if ( interface->interfaceName() == "KDCDiscStructurePlugin" )
		{
			connect( static_cast<KDCPluginInterface*>( interface ), SIGNAL( objectChanged( KDCObject * ) ), this, SLOT( setObject( KDCObject * ) ) );
		}
		else
			if ( interface->interfaceName() == "KDCMediaPlayerFactoryInterface" )
			{
				gKDCMediaPlayer = static_cast<KDCMediaPlayerFactoryInterface*>( interface )->mainInstance();
			}
		else
			if ( interface->interfaceName() == "KDCJobsInterface" )
			{
				gKDCJobsInterface = static_cast<KDCJobsInterface*>( interface );
			}
		else
			if ( interface->interfaceName() == "KDCStatusBarInterface" )
			{
				gKDCStatusBar = static_cast<KDCStatusBarInterface*>( interface );
			}
	setObject( NULL );
	return true;
}

bool KDCChapterEditorPlugin::setInterfaces( QList<KDCBaseInterface*>& aInterfaces )
{
	pInterfaces = &aInterfaces;
	return true;
}

KDCObject* KDCChapterEditorPlugin::currentObject() const
{
	return NULL;
}

void KDCChapterEditorPlugin::refresh()
{

}

void KDCChapterEditorPlugin::setObject( KDCObject *aObject )
{
	if ( aObject && aObject->object() == KDC::Movie )//&& getDisc( aObject )->type() == KDC::DVD )
		pMainWidget->setChapter( static_cast<KDCMovie*>( aObject )->chapter() );
	else
		pMainWidget->setChapter( NULL );
}


#include "kdcchaptereditorplugin.moc"
